package com.macro.mall.portal.service.impl;


import com.macro.mall.mgb.mapper.BrandMapper;
import com.macro.mall.mgb.model.Brand;
import com.macro.mall.mgb.model.Member;
import com.macro.mall.portal.domain.MemberBrandAttention;
import com.macro.mall.portal.repository.MemberBrandAttentionRepository;
import com.macro.mall.portal.service.MemberAttentionService;
import com.macro.mall.portal.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * @author wangwang
 * @version 1.0
 * @description 会员品牌关注管理
 * @date 2025/7/29 21:11
 */
@Service
public class MemberAttentionServiceImpl implements MemberAttentionService {
    @Value("${mongo.insert.sqlEnable}")
    private Boolean sqlEnable; // 用于控制是否通过数据库数据来插入mongo

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private MemberService memberService;

    @Autowired
    private MemberBrandAttentionRepository memberBrandAttentionRepository;

    @Override
    public int add(MemberBrandAttention memberBrandAttention) {
        if(memberBrandAttention.getBrandId() == null){
            return 0;
        }
        Member member = memberService.getCurrentMember();
        memberBrandAttention.setMemberId(member.getId());
        memberBrandAttention.setMemberNickname(member.getNickname());
        memberBrandAttention.setMemberIcon(member.getIcon());
        MemberBrandAttention attention = memberBrandAttentionRepository.findByMemberIdAndBrandId(memberBrandAttention.getMemberId(), memberBrandAttention.getBrandId());
        //如果MongoDB中没有记录，则添加，否则返回0
        if(attention == null){
            if(sqlEnable){
                Brand brand = brandMapper.selectById(memberBrandAttention.getBrandId());
                if(brand == null){
                    return 0;
                }else {
                    memberBrandAttention.setBrandCity(null);
                    memberBrandAttention.setBrandLogo(brand.getLogo());
                    memberBrandAttention.setBrandName(brand.getName());
                }
                memberBrandAttentionRepository.save(memberBrandAttention);
                return 1;
            }
        }
        return 0;
    }

    @Override
    public int delete(Long brandId) {
        Member member = memberService.getCurrentMember();
        return memberBrandAttentionRepository.deleteByMemberIdAndBrandId(member.getId(),brandId);
    }

    @Override
    public MemberBrandAttention detail(Long brandId) {
        Member member = memberService.getCurrentMember();
        return memberBrandAttentionRepository.findByMemberIdAndBrandId(member.getId(), brandId);
    }

    @Override
    public Page<MemberBrandAttention> list(Integer pageNum, Integer pageSize) {
        Member member = memberService.getCurrentMember();
        PageRequest pageRequest = PageRequest.of(pageNum - 1, pageSize);
        return memberBrandAttentionRepository.findByMemberId(member.getId(),pageRequest);
    }

    @Override
    public void clear() {
        Member member = memberService.getCurrentMember();
        memberBrandAttentionRepository.deleteAllByMemberId(member.getId());
    }
}
